package com.j.designpattern.adapter;


/**
 * @version 1.0 created by wangqi_yy on 2020/8/4 9:30 下午
 */
public class CacheAdapterTest3 {
    public static void main(String[] args) {
        CommonCache commonCache = new RedisAdapter(new RedisCommonCache());
        commonCache.get("key1");
        CommonCache commonCache2 = new CaffineAfapter(new CaffineCommonCache());
        commonCache2.get("key1");
        //这样就可以在源头选择是使用redis还是caffine了

    }



}

/**
 * 目标缓存操作
 */
interface CommonCache{

    String get(String key);

    void set(String key, String value);
}

/**
 * redis缓存操作
 */
class RedisCommonCache {

    String get(String key){
        System.out.println("redis客户端get缓存");
        return key;
    }

    void set(String key, String value){
        System.out.println("redis客户端set缓存");
    }
}

/**
 * 内存缓存
 */
class CaffineCommonCache {

    String get(String key){
        System.out.println("Caffine客户端get缓存");
        return key;
    }

    void set(String key, String value){
        System.out.println("Caffine客户端set缓存");
    }
}

/**
 * redis适配器
 */
class RedisAdapter implements CommonCache{
    RedisCommonCache redisCommonCache;

    public RedisAdapter(RedisCommonCache redisCommonCache) {
        this.redisCommonCache = redisCommonCache;
    }
    @Override
    public String get(String key) {
        return redisCommonCache.get(key);
    }

    @Override
    public void set(String key, String value) {
        redisCommonCache.set(key,value
        );
    }
}

class CaffineAfapter implements CommonCache {

    CaffineCommonCache caffineCommonCache;

    public CaffineAfapter(CaffineCommonCache caffineCommonCache) {
        this.caffineCommonCache = caffineCommonCache;
    }
    @Override
    public String get(String key) {
        return caffineCommonCache.get(key);
    }

    @Override
    public void set(String key, String value) {
        caffineCommonCache.set(key,value);
    }
}